<?php
class AppController extends Controller{
    var $transaction;
    
	var $uses = array('User','Meta');
	var $permissions = array();
	
	var $helpers = array('Session','Html','Text','Time','Ajax','Javascript','Js' => array('Jquery'),'Cksource','FileUpload.FileUpload','Google','Country');
    var $components = array( 'Auth','Session','RequestHandler','Email','Cookie','FileUpload.FileUpload','Search.Prg');

    var $presetVars = array(
		array('field' => 'group_id', 'type' => 'value'),
		array('field' => 'search', 'type' => 'value')
    );
    
    function beforeRender(){
    	parent::beforeRender();
    	$meta = $this->Meta->__findCurrentPage(array('url' => $this->here));
    	
    	$this->set('title_for_layout', (isset($meta['Meta']['title'])) ? $meta['Meta']['title'] : null);
    	$this->set('description_for_layout',  (isset($meta['Meta']['description']))?$meta['Meta']['description']:null);
    	$this->set('keywords_for_layout', (isset($meta['Meta']['keywords']))?$meta['Meta']['keywords']:null);
    	 
    }
	
    /**
     * (non-PHPdoc)
     * @see cake/libs/controller/Controller::beforeFilter()
     */
    function beforeFilter() {
		parent::beforeFilter();	

		$this->_setLanguage();       
		$loginRedirect = $this->Session->read('loginRedirect');
		$loginRedirect = (!empty($loginRedirect)) ? $loginRedirect : array('plugin'=>null,'controller' => 'pages', 'action' => 'display'); 
		$this->Auth->authorize = 'controller';
        $this->Auth->loginRedirect = $loginRedirect;
        $this->Auth->logoutRedirect = array('plugin'=>null,'controller' => 'users', 'action' => 'login');
		$this->Auth->loginError = __("Your username and/or password was not recognized. Use the form below in order to retrieve your login details.",true);
   	 	$this->Auth->authError = __("Proszę się zalogować. Strona {$this->params['url']['url']} jest dostępna po zalogowaniu.",true);
        
//   	 	if(!$this->Auth->user()) 
        if($this->isAuthorized()) $this->Auth->allow($this->action);
        $this->transaction = ConnectionManager::getDataSource('default');
        

	}
    
	function _setLanguage() {
		if(!isset($this->params['language'])){
			if ($this->Session->read('Config.language') != false && $this->Session->read('Config.language') != '') {     
				Configure::write('Config.language', $this->Session->read('Config.language'));				
			}else{
				// domy�lny j�zyk
				Configure::write('Config.language', Configure::read('Config.default_lang'));
			}
		}else if(isset($this->params['language']) && $this->params['language'] != Configure::read('Config.language')){
			Configure::write('Config.language', $this->params['language']);
			$this->Session->write('Config.language',$this->params['language']);			
		}else{			
			// domy�lny j�zyk
		    Configure::write('Config.language', Configure::read('Config.default_lang'));
		}
	}
	
    function isAuthorized(){ 
        if($this->Auth->user('group') == 'admin') return true; //Remove this line if you don't want admins to have access to everything by default 
        if(!empty($this->permissions[$this->action])){ 
           	if($this->permissions[$this->action] == '*') return true;            	
            if(in_array($this->Auth->user('group'), $this->permissions[$this->action])) return true;            
        }else{ 
		    // If there are no permissions, let everyone in 		    
		    $this->Auth->allow($this->action); 
		    return true; 
		}
		$this->Session->setFlash($this->Auth->authError);
		
		if($this->Session->check('Auth.User.id')){
			$this->redirect($this->referer());
		}else{
			$this->Session->write('loginRedirect',$this->referer());
			$this->redirect($this->Auth->loginAction);
		}
		
        return false;         
    }
    
    function sendEmail(){}   

    
}